package com.kerrykidz.system.util.printReceipt;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PrinterDao implements Printable {
	private List<PrinterMessage> list;

	public PrinterDao(List<PrinterMessage> list) {
		this.list = list;
	}

	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		System.out.println(list);
		try {
			/**
			 * * @param Graphic指明打印的图形环境 * @param
			 * PageFormat指明打印页格式（页面大小以点为计量单位，1
			 * 点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点） * @param pageIndex指明页号
			 **/
			// 转换成Graphics2D
			Graphics2D g2_1 = (Graphics2D) gra;
			// Graphics2D g2_2 = (Graphics2D) gra;
			// 设置打印颜色为黑色
			g2_1.setColor(Color.black);
			// g2_2.setColor(Color.black);
			// 打印起点坐标
			switch (pageIndex) {
			case 0:
				// 设置打印字体（字体名称、样式和点大小）（字体名称可以是物理或者逻辑名称）
				// Java平台所定义的五种字体系列：Serif、SansSerif、Monospaced、Dialog 和
				// DialogInput
				Font fontBrand = new Font("Arial", Font.BOLD, 20);
				Font font = new Font("新宋体", Font.BOLD, 13);
				Font font2 = new Font("新宋体", Font.PLAIN, 13);
				g2_1.setFont(font2); // 设置字体
				// BasicStroke bs_3=new BasicStroke(0.5f);
				float[] dash1 = { 2.0f };
				// 设置打印线的属性。
				// 1.线宽 2端点的装饰、3应用在路径线段交汇处的装饰，4、空白的宽度，5、虚线的宽度，6、偏移量
				g2_1.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
						BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
				double x = pf.getImageableX();
				double y = pf.getImageableY();
				float heigth1 = font.getSize2D();
				float heigth = font2.getSize2D() + 2; // 字体高度
				PrinterMessage bean1 = list.get(0);
				// 打印菜单头部信息
				g2_1.setFont(fontBrand); // 设置字体
				g2_1.drawString(bean1.getBrand(), (float) x + 20, (float) y);
				System.out.println("--------------------------------------"+bean1.getBrand()+"--------------------------------------");
				g2_1.setFont(font2);
				g2_1.drawString(bean1.getShop(), (float) x + 130, (int) (y));
				g2_1.drawString("", (float) x + 20, (int) (y + 2 * heigth1));
				g2_1.setFont(font2); // 设置字体
				g2_1.drawString("流水号：" + bean1.getOrderNo(), (float) x + 20,
						(float) y + 4 * heigth1);
				g2_1.drawString("收银：" + bean1.getCashier(), (float) x + 320,
						(float) y + 4 * heigth1);
				// 曲线
				g2_1.drawLine((int) x + 10, (int) (y + 5 * heigth1),
						(int) x + 400, (int) (y + 5 * heigth1));
				g2_1.setFont(font);
				g2_1.drawString("商品", (float) x + 60, (float) y + 7 * heigth1);
				// g2_1.setFont(font2); // 设置字体
				g2_1.drawString("数量", (float) x + 190, (float) y + 7 * heigth1);
				// g2_1.setFont(font2); // 设置字体
				g2_1.drawString("单价", (float) x + 260, (float) y + 7 * heigth1);
				// g2_1.setFont(font2); // 设置字体
				g2_1.drawString("金额", (float) x + 345, (float) y + 7 * heigth1);
				int y1 = 0;
				g2_1.setFont(font2);
				// 打印菜单内容
				for (PrinterMessage bean : list) {
					g2_1.drawString(bean.getItemName(), (float) x + 60,
							(float) y + ((float) 7 + y1) * heigth);
					y1++;
					g2_1.drawString(bean.getQty() + "", (float) x + 190,
							(float) y + ((float) 7 + y1) * heigth);
					g2_1.drawString(bean.getPrice() + "", (float) x + 260,
							(float) y + ((float) 7 + y1) * heigth);
					g2_1.drawString(bean.getPrice() + "", (float) x + 345,
							(float) y + ((float) 7 + y1) * heigth);
					y1++;
				}
				y1++;
				// 曲线
				g2_1.setFont(font);
				g2_1.drawLine((int) x + 10, (int) (y + (9 + y1) * heigth1),
						(int) x + 400, (int) (y + (9 + y1) * heigth1));
				
				g2_1.drawString("合计：", (float) x + 60, (float) y
						+ ((float) 7 + y1) * heigth);
				g2_1.setFont(font2);
				g2_1.drawString("￥" + bean1.getCash(), (float) x + 345,
						(float) y + ((float) 7 + y1) * heigth);
				y1++;
				g2_1.drawString("现金：", (float) x + 60, (float) y
						+ ((float) 7 + y1) * heigth);
				g2_1.setFont(font2);
				g2_1.drawString("￥" + bean1.getTotalPrice(), (float) x + 345,
						(float) y + ((float) 7 + y1) * heigth);
				y1++;
				g2_1.drawString("余额：", (float) x + 60, (float) y
						+ ((float) 7 + y1) * heigth);
				g2_1.setFont(font2);
				g2_1.drawString("￥" + bean1.getPaidIn(), (float) x + 345,
						(float) y + ((float) 7 + y1) * heigth);
				y1++;
				g2_1.drawString("刷卡：", (float) x + 60, (float) y
						+ ((float) 7 + y1) * heigth);
				g2_1.setFont(font2);
				g2_1.drawString("￥" + bean1.getPaidIn(), (float) x + 345,
						(float) y + ((float) 7 + y1) * heigth);
				y1++;

				// 曲线
				g2_1.drawLine((int) x + 10, (int) (y + (11 + y1) * heigth1),
						(int) x + 400, (int) (y + (11 + y1) * heigth1));
				y1++;
				g2_1.setFont(font2);
				g2_1.drawString("会员：", (float) x + 20, (float) y
						+ ((float) 12 + y1) * heigth1);
				g2_1.drawString("余额：", (float) x + 260, (float) y
						+ ((float) 12 + y1) * heigth1);
				y1++;
				// 曲线
				g2_1.drawLine((int) x + 10, (int) (y + (13 + y1) * heigth1),
						(int) x + 400, (int) (y + (13 + y1) * heigth1));
				y1++;
				// 时间
				g2_1.drawString("" + bean1.getDate(), (float) x + 20,
						(float) y +((float) 14 + y1) * heigth1);
				g2_1.drawString("签名：", (float) x + 260, (float) y
						+ ((float) 14 + y1) * heigth1);
				return PAGE_EXISTS;
			default:
				return NO_SUCH_PAGE;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 将日期转换成"yyyy-MM-dd HH:mm:ss"格式的字符串
	 * 
	 * @param date
	 *            待格式化的日期
	 * @return 返回格式化后的日期字符串
	 */
	public static String formatDateTime(Date date) {
		String formatStr = "";

		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss");
			formatStr = simpleDateFormat.format(date);
		}

		return formatStr;
	}
}